package com.xinya.wanandroidclient.http

import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.xinya.wanandroidclient.Constants
import com.xinya.wanandroidclient.base.BaseApp.getContext
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File

/**
 *
 * @ClassName:      RetrofitFactory$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/2/24$ 14:12$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
object RetrofitFactory {
    // okHttpClientBuilder 包括请求的日志输出、cookie保存、cache 100M缓存
    private val okHttpClientBuilder =
        OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
            .cookieJar(
                PersistentCookieJar(
                    SetCookieCache(),
                    SharedPrefsCookiePersistor(getContext())
                )
            )
            .cache(Cache(File(getContext().cacheDir, "cache"), 1024 * 1024 * 100))

    fun factory(): Retrofit {
        val client = okHttpClientBuilder.build()
        return Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(Constants.WAN_ANDROID_BASE_URL)
            .build()
    }
}